<template>
  <el-dialog
    v-model="dialogVisible"
    title="批注"
    width="480px"
    :before-close="handleCancel"
    class="comment-dialog"
  >
    <el-form
      ref="formRef"
      :model="formData"
      :rules="rules"
      label-width="60px"
      class="comment-form"
    >
      <el-form-item label="批注" prop="content">
        <el-input
          v-model="formData.content"
          type="textarea"
          :rows="4"
          placeholder="请输入批注内容"
          show-word-limit
          maxlength="500"
          class="comment-textarea"
        />
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, watch, nextTick } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { CommentData } from './types'

// Props
interface Props {
  modelValue: boolean
  initialContent?: string
}

const props = withDefaults(defineProps<Props>(), {
  initialContent: ''
})

// Emits
interface Emits {
  (e: 'update:modelValue', value: boolean): void
  (e: 'confirm', data: string): void
  (e: 'cancel'): void
}

const emit = defineEmits<Emits>()

// 响应式数据
const dialogVisible = ref(false)
const formRef = ref<FormInstance>()
const formData = ref<CommentData>({
  content: ''
})

// 表单验证规则
const rules: FormRules = {
  content: [
    { required: true, message: '请输入批注内容', trigger: 'blur' },
    { min: 1, max: 500, message: '批注内容长度在 1 到 500 个字符', trigger: 'blur' }
  ]
}

// 监听 modelValue 变化
watch(
  () => props.modelValue,
  (newVal) => {
    dialogVisible.value = newVal
    if (newVal) {
      handleOpen()
    }
  },
  { immediate: true }
)

// 监听对话框显示状态
watch(dialogVisible, (newVal) => {
  emit('update:modelValue', newVal)
})

// 处理对话框打开
const handleOpen = () => {
  formData.value = {
    content: props.initialContent
  }
  
  // 重置表单验证状态
  nextTick(() => {
    formRef.value?.clearValidate()
  })
}

// 处理确认
const handleConfirm = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    emit('confirm', formData.value.content)
    dialogVisible.value = false
  } catch (error) {
    console.warn('表单验证失败:', error)
  }
}

// 处理取消
const handleCancel = () => {
  dialogVisible.value = false
  emit('cancel')
}
</script>

<style scoped>
.comment-dialog {
  border-radius: 12px;
}

.comment-dialog :deep(.el-dialog__header) {
  background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
  padding: 20px 24px 16px;
  border-bottom: 1px solid #e2e8f0;
  border-radius: 12px 12px 0 0;
}

.comment-dialog :deep(.el-dialog__title) {
  font-size: 18px;
  font-weight: 600;
  color: #1e293b;
}

.comment-dialog :deep(.el-dialog__body) {
  padding: 24px;
}

.comment-form {
  margin: 0;
}

.comment-form :deep(.el-form-item__label) {
  font-weight: 500;
  color: #374151;
  line-height: 1.5;
}

.comment-textarea :deep(.el-textarea__inner) {
  border-radius: 8px;
  border: 1px solid #d1d5db;
  font-size: 14px;
  line-height: 1.6;
  transition: all 0.2s ease;
  resize: vertical;
  min-height: 100px;
}

.comment-textarea :deep(.el-textarea__inner):focus {
  border-color: #3b82f6;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.comment-textarea :deep(.el-input__count) {
  background: rgba(255, 255, 255, 0.9);
  border-radius: 4px;
  padding: 2px 6px;
  font-size: 12px;
  color: #6b7280;
}

.dialog-footer {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  padding: 16px 24px;
}

.dialog-footer .el-button {
  min-width: 80px;
  border-radius: 6px;
  font-weight: 500;
}

.dialog-footer .el-button--primary {
  background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
  border: none;
  box-shadow: 0 2px 4px rgba(59, 130, 246, 0.3);
}

.dialog-footer .el-button--primary:hover {
  background: linear-gradient(135deg, #2563eb 0%, #1e40af 100%);
  box-shadow: 0 4px 8px rgba(59, 130, 246, 0.4);
}

.dialog-footer .el-button:not(.el-button--primary) {
  background: #fff;
  border: 1px solid #d1d5db;
  color: #374151;
}

.dialog-footer .el-button:not(.el-button--primary):hover {
  border-color: #9ca3af;
  background: #f9fafb;
}
</style> 